package cn.liukuan.jps.parse;

import cn.liukuan.jps.utils.JavassistUtils;
import javassist.ClassPool;
import javassist.CtClass;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.RAMDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;

/**
 * Created by liukuan on 16/5/31.
 */
public class OOSearch {

    static {
        try {
            JavassistUtils.replaceFlatMap("org.apache.lucene.store.RAMDirectory", "fileMap");
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws Exception {


        ClassPool pool = ClassPool.getDefault();

        CtClass ctClass = pool.get("org.apache.lucene.store.RAMDirectory");

        CtClass ctClass1 = ctClass.getClass().newInstance();


        IndexReader reader = DirectoryReader.open(new RAMDirectory());
        IndexSearcher searcher = new IndexSearcher(reader);
        Analyzer analyzer = new IKAnalyzer();

        QueryParser parser = new QueryParser("title", analyzer);
        Query query = parser.parse("经验");
//        Query query = new TermQuery(new Term("name", "b.txt"));

//        Query query = new TermRangeQuery("name", new BytesRef("a"), new BytesRef("z"), true, true);
//        Query query = new TermRangeQuery("title", new BytesRef("a"), new BytesRef("z"), true, true);
//        Query query = LegacyNumericRangeQuery.newIntRange("num", 1, 3, true , true);
//        Query query = LongPoint.newRangeQuery("num", 0, 6);
        TopDocs search = searcher.search(query, 100);

        ScoreDoc[] scoreDocs = search.scoreDocs;

        System.out.println(search.totalHits);

        for (ScoreDoc doc : scoreDocs){
            Document document = searcher.doc(doc.doc);
            System.out.println(document.get("title"));
            System.out.println(document.get("content"));
            System.out.println(document.get("path"));
        }

        reader.close();
    }
}
